namespace Crux.Core.Dapper;

/// <inheritdoc/>
public class ExecuteListener(ILogger<ExecuteListener> logger) :
    IExecuteListener
{
    private readonly ILogger<ExecuteListener> _logger = logger ?? throw new ArgumentNullException(nameof(logger));

    /// <inheritdoc/>
    public void ConnectionClosed(IDbConnection connection, long milliseconds)
    {
        if (_logger.IsEnabled(LogLevel.Information))
        {
            _logger.LogInformation(
                $"ConnectionClosed ({milliseconds}ms)\n ConnectionString:{connection.ConnectionString} ConnectionTimeout:{connection.ConnectionTimeout}");
        }
    }

    /// <inheritdoc/>
    public void ConnectionOpened(IDbConnection connection, long milliseconds)
    {
        if (_logger.IsEnabled(LogLevel.Information))
        {
            _logger.LogInformation(
                $"ConnectionOpened ({milliseconds}ms)\n ConnectionString:{connection.ConnectionString} ConnectionTimeout:{connection.ConnectionTimeout}");
        }
    }

    /// <inheritdoc/>
    public void CommandExecuted(IDbConnection connection, DbCommand command, long milliseconds)
    {
        if (_logger.IsEnabled(LogLevel.Information))
        {
            _logger.LogInformation(
                $"Executed DbCommand ({milliseconds}ms) CommandTimeout({command.CommandTimeout}s)\n{command.CommandText}");
        }
    }

    /// <inheritdoc/>
    public void CommandCancelled(IDbConnection connection, DbCommand command, long milliseconds)
    {
        if (_logger.IsEnabled(LogLevel.Information))
        {
            _logger.LogInformation(
                $"Cancelled DbCommand ({milliseconds}ms) CommandTimeout({command.CommandTimeout}s)\n{command.CommandText}");
        }
    }
}